import java.util.HashMap;

/**
 *  1.两数之和
 */
public class question1 {
    public int[] twoSum(int[] nums, int target) {

//        int p1 = 0;
//        int p2 = nums.length - 1;
//        for (int i = p1; i < p2; i++) {
//            if (nums[p1] + nums[p2] == target) {
//                return new int[]{p1, p2};
//            }else if (nums[p1] + nums[p2] < target) {
//                p1++;
//            }else {
//                p2--;
//            }
//        只适用于有序数组

        int left = 0, right = nums.length - 1;
        for (int i = left; i < right; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }

    public int[] twoSum2(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }
        return null;
    }
}
